package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.exception.DishBusinessException;
import com.itheima.reggie.mapper.DishFlavorsMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Lff
 * @date 2022/3/1
 */
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorsMapper dishFlavorsMapper;

    @Override
    public Page<Dish> findAllByPage(Integer page, Integer pageSize, String name) {
        Page<Dish> pages = new Page<>(page, pageSize);
        LambdaQueryWrapper<Dish> dw = Wrappers.lambdaQuery(Dish.class);
        dw.orderByAsc(Dish::getSort);
        dw.orderByDesc(Dish::getUpdateTime);
        dw.like(StringUtils.isNotEmpty(name), Dish::getName, name);
        dishMapper.selectPage(pages, dw);
        return pages;
    }

    @Override
    @Transactional
    public void saveDish(DishDto dishDto) {
        Dish dish = new Dish();
        //拷贝属性
        BeanUtils.copyProperties(dishDto, dish);
        //添加菜品
//        try {
        dishMapper.insert(dish);
        //通过菜品id添加口味信息
        dishDto.getFlavors().stream().forEach(flavor -> {
            //填充dishId
            flavor.setDishId(dish.getId());
            dishFlavorsMapper.insert(flavor);
        });
//        } catch (Exception e) {
//            throw new DishBusinessException("添加菜品失败,请稍后重试！");
//        }
    }

    @Override
    public DishDto findById(Long id) {
        DishDto dishDto = dishMapper.selectByDto(id);
        //拷贝属性
        LambdaQueryWrapper<DishFlavor> dfqw = Wrappers.lambdaQuery(DishFlavor.class);
        //条件
        dfqw.eq(DishFlavor::getDishId, id);
        //查询并填充
        List<DishFlavor> dishFlavors = dishFlavorsMapper.selectList(dfqw);
        dishDto.setFlavors(dishFlavors);
        return dishDto;
    }


    @Override
    @Transactional
    public void updateWithFlavors(DishDto dishDto) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDto, dish);
        //修改菜品信息
        dishMapper.updateById(dish);
        //先删除所有的口味信息
        LambdaQueryWrapper<DishFlavor> flavorqw = Wrappers.lambdaQuery(DishFlavor.class);
        flavorqw.eq(DishFlavor::getDishId, dishDto.getId());
        dishFlavorsMapper.delete(flavorqw);
        dishDto.getFlavors().stream().forEach(flavors -> {
            //修改菜品口味信息
            flavors.setDishId(dishDto.getId());
            dishFlavorsMapper.insert(flavors);
        });
    }

    @Override
    @Transactional
    public void delByIds(Long[] ids) {
        LambdaQueryWrapper<Dish> qw = Wrappers.lambdaQuery(Dish.class);
        qw.in(Dish::getId, ids);
        //停售才能删除
        qw.eq(Dish::getStatus, 0);
        if (dishMapper.delete(qw) != ids.length) { //删除不数量一致
            throw new DishBusinessException("包含在售商品，删除失败。。");
        }
        LambdaQueryWrapper<DishFlavor> dfqw = Wrappers.lambdaQuery(DishFlavor.class);
        dfqw.in(DishFlavor::getDishId, ids);
        dishFlavorsMapper.delete(dfqw);
    }

    @Override
    public void updateDishStatus(Integer status, Long[] ids) {
        LambdaUpdateWrapper<Dish> dqw = Wrappers.lambdaUpdate(Dish.class);
        dqw.set(Dish::getStatus, status);
        dqw.in(Dish::getId, ids);
        Dish dish = new Dish();
        dish.setStatus(status);
        dishMapper.update(dish, dqw);
    }

    @Override
    public List<DishDto> getListByCategoryId(Dish dish) {
        LambdaQueryWrapper<Dish> dlqw = Wrappers.lambdaQuery(Dish.class);
        dlqw.eq(Dish::getCategoryId, dish.getCategoryId());
        dlqw.orderByAsc(Dish::getSort);
        dlqw.orderByDesc(Dish::getUpdateTime);
        List<Dish> dishes = dishMapper.selectList(dlqw);
        List<DishDto> dishDtos = dishes.stream().map(dish1 -> {
            DishDto dishDto = new DishDto();
            //拷贝属性
            BeanUtils.copyProperties(dish1,dishDto);
            //查询口味表
            LambdaQueryWrapper<DishFlavor> dqw = Wrappers.lambdaQuery(DishFlavor.class);
            dqw.eq(DishFlavor::getDishId,dish1.getId());
            List<DishFlavor> dishFlavors = dishFlavorsMapper.selectList(dqw);
            dishDto.setFlavors(dishFlavors);
            return dishDto;
        }).collect(Collectors.toList());
        return dishDtos;
    }
}
